/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import dominio.Categoria;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import persistencia.JpaUtil;

/**
 *
 * @author fabricio
 */
public class GeraBanco {

    private static Logger LOGGER = Logger.getLogger(GeraBanco.class.getName());

    public static void main(String[] args) throws Throwable {

        EntityManager em = null;
        EntityTransaction trans = null;
        Categoria categoria = null;

        try {
            em = JpaUtil.getEntityManagerFactory().createEntityManager();

            trans = em.getTransaction();
            trans.begin();

            categoria = new Categoria();
            categoria.setNome("JavaEE");
            categoria = em.merge(categoria);

            List<Categoria> lista = em.createQuery("from Categoria").getResultList();
            for (Categoria cat : lista) {
                System.out.println(cat.getNome());
            }

            trans.commit();

        } catch (Throwable th) {
            if (trans != null) {
                try {
                    trans.rollback();
                } catch (Throwable th2) {
                    LOGGER.log(Level.SEVERE, null, th2);
                }
            }
            throw th;

        } finally {
            if (em != null) {
                try {
                    em.close();
                } catch (Throwable th2) {
                    LOGGER.log(Level.SEVERE, null, th2);
                }
            }
        }

        try {
            em = JpaUtil.getEntityManagerFactory().createEntityManager();

            trans = em.getTransaction();
            trans.begin();

            categoria = em.merge(categoria);

            List<Categoria> lista = em.createQuery("from Categoria").getResultList();
            for (Categoria cat : lista) {
                System.out.println(cat.getNome());
            }

            trans.commit();

        } catch (Throwable th) {
            if (trans != null) {
                try {
                    trans.rollback();
                } catch (Throwable th2) {
                    LOGGER.log(Level.SEVERE, null, th2);
                }
            }
            throw th;

        } finally {
            if (em != null) {
                try {
                    em.close();
                } catch (Throwable th2) {
                    LOGGER.log(Level.SEVERE, null, th2);
                }
            }
        }

    }
}
